/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.bean.IncomeBillBean;
import com.minierp.bean.UserBean;
import com.minierp.dao.BankAccountBalanceDao;
import com.minierp.dao.BankAccountDao;
import com.minierp.dao.CodeRuleDao;
import com.minierp.dao.DepartmentDao;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.IncomeBillDao;
import com.minierp.dao.IncomeTypeDao;
import com.minierp.dao.PayModeDao;
import com.minierp.dao.SerialNumberDao;
import com.minierp.err.PermissionDennyException;
import com.minierp.helper.EncCommonHelper;
import com.minierp.helper.PageHelper;
import com.minierp.model.CodeRule;
import com.minierp.model.EnterpriseInfo;
import com.minierp.model.IncomeBill;
import com.minierp.model.TallySet;
import com.minierp.service.IncomeBillService;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.dao.CannotAcquireLockException;
import org.springframework.util.FileCopyUtils;
import org.springframework.validation.BindException;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;

public class IncomeBillFormCtr extends SimpleFormController {
	private DepartmentDao departmentDao;
	private BankAccountDao bankAccountDao;
	private IncomeBillDao incomeBillDao;
	private CodeRuleDao codeRuleDao;
	private SerialNumberDao serialNumberDao;
	private BankAccountBalanceDao bankAccountBalanceDao;
	private PayModeDao payModeDao;
	private IncomeTypeDao incomeTypeDao;
	private IdGenerator idGenerator;
	private IncomeBillService incomeBillService;

	protected Map referenceData(HttpServletRequest request) throws Exception {
		HashMap refData = new HashMap();
		String id = request.getParameter("id");
		if (id == null) {
			id = "";
		}

		refData.put("id", id);
		String retVal = request.getParameter("ret_val");
		if (retVal == null) {
			retVal = "";
		}

		refData.put("retVal", retVal);
		refData.put("departmentList", this.departmentDao.getList(true));
		List baList = this.bankAccountDao.getList("", "");
		EnterpriseInfo ent = EncCommonHelper.getEnterpriseInfo(request);
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		long dfTsId = 0L;
		long specId = 0L;
		if (id != null && !id.equals("")) {
			IncomeBill bb = (IncomeBill) this.incomeBillDao.get(
					IncomeBill.class, Long.parseLong(id));
			specId = bb.getBankAccountId();
			dfTsId = bb.getTallySetId();
		} else {
			dfTsId = ub.getUser().getDfTsId();
		}

		refData.put("bankAccountList",
				PageHelper.parseBaList(baList, ent, ub, dfTsId, specId));
		refData.put("incomeTypeList", this.incomeTypeDao.getList(false));
		return refData;
	}

	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		String id = request.getParameter("id");
		CodeRule cr = this.codeRuleDao.get("INCOME_BILL");
		if (cr == null) {
			throw new Exception("Code rule not found for income bill");
		} else {
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
			IncomeBillBean form = new IncomeBillBean();
			form.setCodeRule(cr);
			if (id != null && !id.equals("")) {
				IncomeBill templateId1 = (IncomeBill) this.incomeBillDao.get(
						IncomeBill.class, Long.parseLong(id));
				if (templateId1 == null) {
					throw new Exception("Record not found for id:" + id);
				}

				TallySet incomeBill1 = (TallySet) this.incomeBillDao.get(
						TallySet.class, templateId1.getTallySetId());
				form.setSychronizeValue(templateId1.getUpdateTime().getTime());
				if (incomeBill1 != null) {
					form.setTallySetName(incomeBill1.getTitle());
				}

				form.setOldId(templateId1.getId());
				form.setCostStr(templateId1.getCost().toString());
				form.setIncomeDateStr(sf.format(templateId1.getIncomeDate()));
				form.setIncomeBill(templateId1);
				if (templateId1.getAttachFile() != null
						&& !templateId1.getAttachFile().equals("")) {
					int ub1 = templateId1.getAttachFile().lastIndexOf("/");
					form.setAttachFileName(templateId1.getAttachFile()
							.substring(ub1 + 1));
				}
			} else {
				if (!EncCommonHelper.checkPrivilege(request, "INCO_BILL")) {
					throw new PermissionDennyException("Permission Denny");
				}

				String templateId = request.getParameter("template_id");
				IncomeBill incomeBill = new IncomeBill();
				UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
				if (templateId != null && !templateId.equals("")) {
					IncomeBill tf = (IncomeBill) this.incomeBillDao.get(
							IncomeBill.class, Long.parseLong(templateId));
					if (tf == null) {
						throw new Exception(
								"Template IncomeBill not found for id "
										+ templateId);
					}

					incomeBill.setBankAccountId(tf.getBankAccountId());
					incomeBill.setDepartmentId(tf.getDepartmentId());
					incomeBill.setIncomeDate(tf.getIncomeDate());
					incomeBill.setIncomeTypeId(tf.getIncomeTypeId());
					incomeBill.setManagerId(tf.getManagerId());
					incomeBill.setManagerName(tf.getManagerName());
					incomeBill.setMark(tf.getMark());
					incomeBill.setPayModeId(tf.getPayModeId());
					incomeBill.setPayerName(tf.getPayerName());
				} else if (ub.getStaffer() != null) {
					incomeBill.setManagerId(ub.getStaffer().getId());
					incomeBill.setManagerName(ub.getStaffer().getStafferName());
					incomeBill.setDepartmentId(ub.getStaffer()
							.getDepartmentId());
				}

				if (ub.getStaffer() != null) {
					incomeBill.setCreaterId(ub.getStaffer().getId());
					incomeBill.setCreaterName(ub.getStaffer().getStafferName());
				}

				incomeBill.setTallySetId(ub.getUser().getDfTsId());
				form.setTallySetName(ub.getDfTsName());
				form.setIncomeDateStr(sf.format(incomeBill.getIncomeDate()));
				form.setIncomeBill(incomeBill);
			}

			return form;
		}
	}

	protected void onBind(HttpServletRequest request, Object command)
			throws Exception {
	}

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		ModelAndView mv = new ModelAndView(this.getSuccessView());
		String id = request.getParameter("id");
		String examine = request.getParameter("examine");
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		IncomeBillBean form = (IncomeBillBean) command;
		IncomeBill obj = form.getIncomeBill();
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		MultipartFile multipartFile = multipartRequest.getFile("attach_file");
		String continueFlag;
		String alertType;
		String printFlag;
		if (multipartFile != null && !multipartFile.isEmpty()) {
			continueFlag = multipartFile.getOriginalFilename();
			alertType = EncCommonHelper.getExtendName(continueFlag);
			printFlag = EncCommonHelper.getUploadVirtualPath(this
					.getServletContext());
			String realPath = this.getServletContext().getRealPath(printFlag);
			String fileName = ub.getUser().getId() + "_"
					+ System.currentTimeMillis() + "." + alertType;
			FileCopyUtils.copy(multipartFile.getBytes(), new File(realPath
					+ System.getProperty("file.separator") + fileName));
			obj.setAttachFile(printFlag + fileName);
		}

		if (examine != null && examine.equals("1")) {
			if (!EncCommonHelper.checkPrivilege(request, "INCO_EXAM")) {
				throw new Exception("Permission deny!");
			}

			obj.setExamined("Y");
			if (ub.getStaffer() != null) {
				obj.setExaminerId(ub.getStaffer().getId());
				obj.setExaminerName(ub.getStaffer().getStafferName());
			}
		} else {
			obj.setExamined("N");
		}

		if (id != null && !id.equals("")) {
			if (!EncCommonHelper.checkPrivilege(request, "INCO_BILL")) {
				throw new Exception("Permission deny!");
			}

			try {
				this.incomeBillService.txUpdate(form, this.incomeBillDao,
						this.idGenerator, this.bankAccountBalanceDao);
			} catch (CannotAcquireLockException arg17) {
				EncCommonHelper.setAlertMessage(request, "事务冲突故障，请重新操作！");
				return this.showForm(request, errors, this.getFormView());
			}
		} else {
			try {
				this.incomeBillService.txInsert(form, this.incomeBillDao,
						this.serialNumberDao, this.idGenerator,
						this.bankAccountBalanceDao);
			} catch (CannotAcquireLockException arg18) {
				EncCommonHelper.setAlertMessage(request, "事务冲突故障，请重新操作！");
				return this.showForm(request, errors, this.getFormView());
			}
		}

		mv.addObject("returnValue", String.valueOf(obj.getId()));
		continueFlag = request.getParameter("continue_flag");
		alertType = request.getParameter("alert_type");
		printFlag = request.getParameter("print_flag");
		if (continueFlag != null && !continueFlag.equals("")) {
			if (continueFlag.equals("NEW")) {
				EncCommonHelper.setAlertMessage(request, "收入单保存成功！");
				mv = new ModelAndView(
						"redirect:/finance/IncomeBillNew.ihtm?ret_val=1&alert_type="
								+ alertType);
			} else if (continueFlag.equals("COPY")) {
				EncCommonHelper.setAlertMessage(request, "收入单保存成功！");
				mv = new ModelAndView(
						"redirect:/finance/IncomeBillNew.ihtm?ret_val=1&template_id="
								+ obj.getId() + "&alert_type=" + alertType);
			}
		} else if (printFlag != null && printFlag.equals("1")) {
			mv = new ModelAndView(
					"redirect:/finance/IncomeBillPrint.ihtm?ret_val=1&id="
							+ obj.getId());
		} else if (alertType == null || !alertType.equals("NONE")) {
			EncCommonHelper.setAlertMessage(request, "收入单保存成功！");
			if (alertType != null && alertType.equals("FORM")) {
				mv = new ModelAndView(new RedirectView(
						"/finance/IncomeBillEdit.ihtm?ret_val=1&id="
								+ obj.getId() + "&alert_type=" + alertType));
			}
		}

		mv.addObject("alertType", alertType);
		return mv;
	}

	public CodeRuleDao getCodeRuleDao() {
		return this.codeRuleDao;
	}

	public void setCodeRuleDao(CodeRuleDao codeRuleDao) {
		this.codeRuleDao = codeRuleDao;
	}

	public DepartmentDao getDepartmentDao() {
		return this.departmentDao;
	}

	public void setDepartmentDao(DepartmentDao departmentDao) {
		this.departmentDao = departmentDao;
	}

	public IdGenerator getIdGenerator() {
		return this.idGenerator;
	}

	public void setIdGenerator(IdGenerator idGenerator) {
		this.idGenerator = idGenerator;
	}

	public SerialNumberDao getSerialNumberDao() {
		return this.serialNumberDao;
	}

	public void setSerialNumberDao(SerialNumberDao serialNumberDao) {
		this.serialNumberDao = serialNumberDao;
	}

	public BankAccountDao getBankAccountDao() {
		return this.bankAccountDao;
	}

	public void setBankAccountDao(BankAccountDao bankAccountDao) {
		this.bankAccountDao = bankAccountDao;
	}

	public IncomeBillDao getIncomeBillDao() {
		return this.incomeBillDao;
	}

	public void setIncomeBillDao(IncomeBillDao incomeBillDao) {
		this.incomeBillDao = incomeBillDao;
	}

	public IncomeBillService getIncomeBillService() {
		return this.incomeBillService;
	}

	public void setIncomeBillService(IncomeBillService incomeBillService) {
		this.incomeBillService = incomeBillService;
	}

	public BankAccountBalanceDao getBankAccountBalanceDao() {
		return this.bankAccountBalanceDao;
	}

	public void setBankAccountBalanceDao(
			BankAccountBalanceDao bankAccountBalanceDao) {
		this.bankAccountBalanceDao = bankAccountBalanceDao;
	}

	public IncomeTypeDao getIncomeTypeDao() {
		return this.incomeTypeDao;
	}

	public void setIncomeTypeDao(IncomeTypeDao incomeTypeDao) {
		this.incomeTypeDao = incomeTypeDao;
	}

	public PayModeDao getPayModeDao() {
		return this.payModeDao;
	}

	public void setPayModeDao(PayModeDao payModeDao) {
		this.payModeDao = payModeDao;
	}
}